package com.ccnf.web.controller;

import com.ccnf.core.bean.Response;
import com.ccnf.core.bean.ResponseWithData;
import com.ccnf.model.entity.InvitationAuth;
import com.ccnf.service.InvitationAuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "InvitationAuthController", description = "邀请函授权用户相关API")
@RestController
@RequestMapping("/invitations")
public class InvitationAuthController {
    @Autowired
    private InvitationAuthService invitationAuthService;

    @ApiOperation("录入邀请函授权信息，返回授权ID")
    @PostMapping("auth")
    public ResponseWithData save(@RequestBody InvitationAuth invitationAuth) {
        return ResponseWithData.success(invitationAuthService.saveInvitationAuth(invitationAuth));
    }

    @ApiOperation("通过授权ID修改授权信息，主要用于录入授权用户的ID")
    @PatchMapping("auth/{authId}")
    public Response modify(@PathVariable Long authId, @RequestBody InvitationAuth invitationAuth) {
        invitationAuth.setAuthId(authId);
        invitationAuthService.modifyInvitationAuth(invitationAuth);
        return Response.success();
    }

    @ApiOperation("通过邀请函ID获取授权的用户列表")
    @GetMapping("{invitationId}/auth")
    public ResponseWithData authUsers(@PathVariable Long invitationId) {
        return ResponseWithData.success(invitationAuthService.findInvitationAuthUsersByInvitationId(invitationId));
    }

    @ApiOperation("通过邀请函ID和用户ID获取邀请函授权信息，如果不存在则返回null")
    @GetMapping("{invitationId}/users/{userId}/auth")
    public ResponseWithData invitationAuthUser(@PathVariable("invitationId") Long invitationId,
                                             @PathVariable("userId") Long userId) {
        return ResponseWithData.success(invitationAuthService.findInvitationAuthByInvitationIdAndUserId(invitationId, userId));
    }

    @ApiOperation("批量删除邀请函授权人信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "invitationId", value = "邀请函ID", required = true, paramType = "path"),
            @ApiImplicitParam(name = "userId", value = "当前用户ID", required = true, paramType = "path"),
            @ApiImplicitParam(name = "authIds", value = "授权ID数组", required = true, paramType = "query")
    })
    @DeleteMapping("{invitationId}/users/{userId}/auth")
    public Response removeAuthUsers(@PathVariable("invitationId") Long invitationId,
                                    @PathVariable("userId") Long userId,
                                    Long[] authIds) {
        invitationAuthService.removeAuthUsers(invitationId, userId, authIds);
        return Response.success();
    }
}